<template>
  <div>
    <Modal v-model="show" :title="getTitle()" :mask-closable="false" :closable="false">
      <Form ref="modalForm" :model="enterprise" :rules="ruls" :label-width="80">
        <FormItem v-if="enterprise.id" label="企业编号" prop="id">
          <Input disabled v-model.trim="enterprise.id" type="text" autocomplete="false"></Input>
        </FormItem>
        <FormItem label="企业名称" prop="name">
          <Input v-model.trim="enterprise.name" type="text" autocomplete="false"></Input>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="default" :disabled="loading" @click="cancel()">取消</Button>
        <Button type="primary" :loading="loading" @click="ok">确定</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
export default {
  data() {
    return {
      show: true,
      loading: false,
      reload: false,
      ruls: {
        name: [
          { required: true, message: "请填写企业名称" },
          {
            max: 20,
            message: "企业名称长度不能超过20"
          }
        ]
      }
    };
  },
  props: {
    enterprise: {
      type: Object,
      default: {}
    }
  },
  methods: {
    /**
     * @description 关闭Modal
     * @param reload 是否重新加载数据
     */
    cancel() {
      this.$emit("cancel", "edit", this.reload);
    },
    getTitle() {
      if (this.enterprise && this.enterprise.id) {
        return "修改企业信息";
      } else {
        return "新建企业";
      }
    },
    ok() {
      this.$refs.modalForm.validate(valid => {
        if (valid) {
          this.editEnterpise({
            id: this.enterprise.id,
            name: this.enterprise.name
          });
        }
      });
    },
    editEnterpise(data) {
      this.loading = true;
      var url = "/enterprise/add";
      var pathVarJson = {};
      if (data.id) {
        url = "/enterprise/edit/{id}";
        pathVarJson = { id: data.id };
      }
      this.post(url, data, pathVarJson).then(res => {
        this.loading = false;

        if (this.checkIsSsuccess(res)) {
          this.reload = true;
          this.$Message.success({
            content: data.id?"修改企业信息成功":"新建企业成功",
            duration: 2,
            closable: true,
            onClose: () => {
              this.cancel();
            }
          });
        } else {
          this.$Message.error({
            content: res["msg"],
            duration: 5,
            closable: true
          });
        }
      });
    },add(data){
      this.loading = true;
      var url = "/enterprise/add";
      var pathVarJson = {};
      this.post(url, data, pathVarJson).then(res => {
        this.loading = false;

        if (this.checkIsSsuccess(res)) {
          this.reload = true;
          this.$Message.success({
            content: "新建企业成功",
            duration: 2,
            closable: true,
            onClose: () => {
              this.cancel();
            }
          });
        } else {
          this.$Message.error({
            content: res["msg"],
            duration: 5,
            closable: true
          });
        }
      });
    }
  },
  created() {}
};
</script>
